local Benzhen = {}

Benzhen.__index = Benzhen

local benzhenLayer = require "Benzhen/BenzhenLayer"
local benzhenRoomType = require "Benzhen/BenzhenRoomType"

function Benzhen:new()
	local object = {}
	setmetatable(object, Benzhen)

	return object
end

function Benzhen.Start(gameObject)
	local objectTransform = UnityEngine.GameObject.GetComponent(gameObject,typeof(UnityEngine.Transform))
	local object = Benzhen:new()
	object.gameObject = gameObject
	object.transform = objectTransform

	object.layer = UnityEngine.Transform.Find(objectTransform, "startPoint")
	local layer1 = UnityEngine.Transform.Find(objectTransform, "startPoint1")
	local layer2 = UnityEngine.Transform.Find(objectTransform, "startPoint2")
	if(object.layer ~= nil)
	then
		local layer = benzhenLayer:new()
		layer:config(object.layer.localPosition, layer1.localPosition, layer2.localPosition)
		local position = object.layer.localPosition
		layer:setPosition(position.x, position.y)
		object.benzhenLayer = layer
	else
		print("startPoint not find")
	end

	gameInstance.benzhen = object
	gameInstance.benzhen:findFloorImage(object.layer)
	gameInstance.benzhen:loadRooms()
	gameInstance.benzhen:loadRoomMenu()

	--gameInstance.benzhen.floorImage.gameObject:SetActive(true)

	local function onScroll(eventData)
		if(eventData:IsScrolling())
			then
			--print("onScroll ", eventData.scrollDelta.x, eventData.scrollDelta.y)
			local scale = object.transform.localScale
			scale.x = scale.x + eventData.scrollDelta.y * 0.1
			if(scale.x < 0.5)
				then
				scale.x = 0.5
				elseif(scale.x > 2)
				then
				scale.x = 2
			end
			scale.y = scale.x
			gameObject.transform.localScale = scale
		end
	end

	local luatouch = gameObject:GetComponent(typeof(LuaTouchComponent))
	luatouch:RegisterFunction(UnityEngine.EventSystems.EventTriggerType.Scroll, onScroll)

	return object
end

function Benzhen.Destroy(gameObject, object)
	-- body
	print("Benzhen Destroy")
end

function Benzhen:findFloorImage(layer)
	local floorImage = UnityEngine.Transform.Find(layer, "floorImage")
	if(floorImage ~= nil) then
		self.floorImage = floorImage
		self.floorImage.gameObject:SetActive(false)
	else
		print("floorImage not find")
	end

end

function Benzhen:loadRooms()

	local iter, roomData = GameData.benzhenData.roomList:next(nil)
	while (iter ~= nil)
	do
		print("init room, room type is "..roomData.roomType)
		self:loadRoom(roomData)
		iter, roomData = GameData.benzhenData.roomList:next(iter)
	end

end

function Benzhen:loadRoom(roomData)

	local gameObject = UnityEngine.Resources.Load("UIPrefab/Room")
	local ins = UnityEngine.Object.Instantiate(gameObject)
	ins.name = benzhenRoomType[roomData.roomType]
	local transform = UnityEngine.GameObject.GetComponent(ins, typeof(UnityEngine.Transform))
	transform:SetParent(self.layer)
	local x, y = self.benzhenLayer:getPosition(roomData.roomPos.x, roomData.roomPos.y, roomData.grid.x, roomData.grid.y)
	transform.localPosition = UnityEngine.Vector3(x,y,0)
	local image = UnityEngine.GameObject.GetComponent(ins, typeof(UnityEngine.UI.Image))
	local imageName = benzhenRoomType[roomData.roomType]
	local roomTex = UnityEngine.Resources.Load("RoomImage/"..imageName, typeof(UnityEngine.Sprite))
	--local roomSprite = UnityEngine.Sprite.Create(roomTex, UnityEngine.Rect(0,0,300,200), UnityEngine.Vector2(0.5,0.5))
	image.sprite = roomTex
end

function Benzhen:loadRoomMenu()
	local gameObject = UnityEngine.Resources.Load("UIPrefab/RoomMenu")
	local objectInstance = UnityEngine.Object.Instantiate(gameObject)
	local transform = objectInstance:GetComponent(typeof(UnityEngine.Transform))
	transform:SetParent(self.layer)
end

return Benzhen